package com.extremeforce.module;

import java.util.ArrayList;
import java.util.HashMap;

import com.extremeforce.R;
import com.extremeforce.adapter.ModuleListAdapter;
import com.extremeforce.adapter.SubscribeListAdapter;
import com.extremeforce.db.DBMaster;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;



public class SubscribeActivity extends Activity{

	    private GridView gridView ;
	    private ArrayList<HashMap<String,String>> list;
		
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.subscribelayout);
	        
	        gridView = (GridView)this.findViewById(R.id.subscribeGridView);
	        
	        list = new ArrayList<HashMap<String,String>>();
	        refreshList();
	        
	        
	        gridView.setAdapter(new SubscribeListAdapter(this,list));
	        
	        gridView.setOnItemClickListener(new ItemClickListener());
	        
	    }
	    
	    private class ItemClickListener implements OnItemClickListener{

			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long id) {
				
				HashMap<String,String> map = list.get(position);
				
				Intent intent = new Intent();
				intent.setClass(SubscribeActivity.this,DeSubscribeActivity.class);
				intent.putExtra("GOODS_ID",map.get("GOODS_ID"));
				intent.putExtra("GOODS_NAME",map.get("GOODS_NAME"));
				intent.putExtra("EXEC_TIME",map.get("EXEC_TIME"));
				intent.putExtra("GOODS_DESC",map.get("GOODS_DESC"));
				
				SubscribeActivity.this.startActivityForResult(intent,11);
			}
	    	
	    }
	    
	    
		@Override
		protected void onActivityResult(int requestCode, int resultCode,
				Intent data) {
			if(requestCode == 11 && data != null ){
				Bundle bundle = data.getBundleExtra("HAS_CHANGED");
				if(bundle != null && bundle.getBoolean("CHANGED")){
					refreshList();
				}
			}
		}
		
		
		private void refreshList(){
			
			list.clear();
			
			DBMaster dbMaster = new DBMaster(this);
			Cursor cursor = dbMaster.getSubscribedGoods();
			while(cursor.moveToNext()){
				String goodsId = cursor.getString(cursor.getColumnIndex("GOODS_ID"));
				String execTime = cursor.getString(cursor.getColumnIndex("EXEC_TIME"));
				String goodsName = cursor.getString(cursor.getColumnIndex("GOODS_NAME"));
				String goodsDesc = cursor.getString(cursor.getColumnIndex("GOODS_DESC"));
				
				HashMap<String,String> map = new HashMap<String,String>();
				
				map.put("GOODS_ID",goodsId);
				map.put("EXEC_TIME",execTime);
				map.put("GOODS_NAME",goodsName);
				map.put("GOODS_DESC",goodsDesc);
				
				
				list.add(map);
			}
			
			dbMaster.close();
			gridView.setAdapter(new SubscribeListAdapter(this,list));
		}	    
}
